package weather;

import java.sql.*;

public class DatabaseHelper {
	private Connection connect = null;
	private Statement stmt = null;
	private PreparedStatement updateValue = null;
	private PreparedStatement getValue=null;
	private ResultSet rs=null;
	
	//Data Base Info
	private String dbName="root";
	private String tableName="wthr_table";
	private String dbPassword="";
	
	//Needed Queries
	String updateWind = "UPDATE "+ tableName +" SET wind = ? WHERE time = ?";
	String getWind = "SELECT wind FROM "+ tableName +" WHERE time =?";
	String updateTemp = "UPDATE "+ tableName +" SET temp = ? WHERE time = ?";
	String getTemp = "SELECT temp FROM "+ tableName +" WHERE time =?";
	
	public void dbAddTemp(int time,double temp) throws Exception{
		try{
			//Establish Connection
			Class.forName("com.mysql.jdbc.Driver");
			connect = DriverManager.getConnection("jdbc:mysql://localhost/weather", dbName, dbPassword);
			/*if(connect!=null){
				System.out.println("Connection Successful");
			}
			else{
				System.out.println("connection Failed");
			}*/
			
			//Update Temp
			stmt=connect.createStatement();
			updateValue = connect.prepareStatement(updateTemp);
			updateValue.setDouble(1, temp);
			updateValue.setInt(2, time);
			updateValue.executeUpdate();
		}
		catch(Exception e){
			//System.out.println("Problem: " + e.toString());
			throw e;
		}
		finally{
			connect.close();
		}
	}
	public double dbGetTemp(int time) throws Exception{
		double result=0;
		try{
			//Establish Connection
			Class.forName("com.mysql.jdbc.Driver");
			connect = DriverManager.getConnection("jdbc:mysql://localhost/weather", dbName, dbPassword);
			/*if(connect!=null){
				System.out.println("Connection Successful");d
			}
			else{
				System.out.println("connection Failed");
			}*/
			
			//Retrieve Temp
			stmt=connect.createStatement();
			getValue=connect.prepareStatement(getTemp);
			getValue.setInt(1, time);
			rs=getValue.executeQuery();
			rs.first();
			{
				result=rs.getDouble("temp");
			}
		}	
		catch(Exception e){
			//System.out.println("Problem: " + e.toString());
			throw e;
		}
		finally{
			connect.close();
			rs.close();
		}
		return result;
	}
	public void dbAddWind(int time, double wind) throws Exception{
		if(wind<0){
			System.out.println("Wind may not be a negative value");
			return;
		}
		try{
			//Establish Connection
			Class.forName("com.mysql.jdbc.Driver");
			connect = DriverManager.getConnection("jdbc:mysql://localhost/weather", dbName, dbPassword);
			/*if(connect!=null){
				System.out.println("Connection Successful");
			}
			else{
				System.out.println("connection Failed");
			}*/
			
			//Update Wind
			stmt=connect.createStatement();
			updateValue = connect.prepareStatement(updateWind);
			updateValue.setDouble(1, wind);
			updateValue.setInt(2, time);
			updateValue.executeUpdate();
		}	
		catch(Exception e){
			//System.out.println("Problem: " + e.toString());
			throw e;
		}
		finally{
			connect.close();
		}
	}
	public double dbGetWind(int time) throws Exception{
		double result=0;
		try{
			//Establish Connection
			Class.forName("com.mysql.jdbc.Driver");
			connect = DriverManager.getConnection("jdbc:mysql://localhost/weather", dbName, dbPassword);
			/*if(connect!=null){
				System.out.println("Connection Successful");
			}
			else{
				System.out.println("connection Failed");
			}*/
			
			//Get Wind
			stmt=connect.createStatement();
			getValue=connect.prepareStatement(getWind);
			getValue.setInt(1, time);
			rs=getValue.executeQuery();
			rs.first();
			{
				result=rs.getDouble("wind");
			}
		}	
		catch(Exception e){
			//System.out.println("Problem: " + e.toString());
			throw e;
		}
		finally{
			connect.close();
			rs.close();
		}
		return result;
	}
	
	public void incrementWindDB () throws Exception
    {
    	DatabaseHelper helper = new DatabaseHelper();
    	int i =0;
    	double value=0;
    	double dbData[] = new double[13];
    	while(i<13)
    	{
    		dbData[i]=helper.dbGetWind(i+1);
    		i++;
    	}
    	i=0;
    	while(i<13)
    	{
    		helper.dbAddWind(i+2, dbData[i]);
    		i++;
    	}
    	return;
    }
	
	public void incrementTempDB () throws Exception
    {
    	DatabaseHelper helper = new DatabaseHelper();
    	int i =0;
    	double value=0;
    	double dbData[] = new double[24];
    	while(i<24)
    	{
    		dbData[i]=helper.dbGetTemp(i+1);
    		i++;
    	}
    	i=0;
    	while(i<24)
    	{
    		helper.dbAddTemp(i+2, dbData[i]);
    		i++;
    	}
    	return;
    }
	
	public void dbClear() throws Exception
	{
		DatabaseHelper helper = new DatabaseHelper();
		try{
			//Reset Values to 0
			int temp = 0;
			int i=0;
			while(i<24){
				helper.dbAddTemp(i+1, 0);
				helper.dbAddWind(i+1, 0);
				i++;
			}
		}	
		catch(Exception e){
			//System.out.println("Problem: " + e.toString());
			throw e;
		}

	}
	public void initDB() throws Exception{
		try{
			//Establish Connection
			Class.forName("com.mysql.jdbc.Driver");
			connect = DriverManager.getConnection("jdbc:mysql://localhost/weather", dbName, dbPassword);
			/*if(connect!=null){
				System.out.println("Connection Successful");
			}
			else{
				System.out.println("connection Failed");
			}*/
			
			//Perform Query
			stmt=connect.createStatement();
			int temp = 0;
			int i=0;
			while(i<24){
				temp=stmt.executeUpdate("INSERT INTO "+ tableName +" (wind,temp) VALUES (0,0)");
				i++;
			}
		}	
		catch(Exception e){
			//System.out.println("Problem: " + e.toString());
			throw e;
		}
		finally{
			connect.close();
		}
	}
}